Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show the admin/settings menu for any of its elements #3783

Merged

Conversation

elia
Copy link
Member

@elia elia commented Oct 1, 2020

Description

It's possible to create permission sets the only give access to a
subset of the settings menu items, without this the settings menu was
kept hidden.

Checklist:

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • I have updated Guides and README accordingly to this change (if needed)
  • I have added tests to cover this change (if needed)
  • I have attached screenshots to this PR for visual changes (if needed)

@elia elia force-pushed the elia/fix-settings-menu-condition branch from e7bc874 to 6e20af2 Compare October 1, 2020 15:15
Comment on lines 28 to 30
view = double("view", can?: false)
expect(view).to receive(:can?).at_least(12).times
view.instance_exec(&menu_item.condition)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to test the result of view.instance_exec(&menu_item.condition) in a couple of different scenarios? With your current test, one could change all the || to && and still have the test pass.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, this is a trade-off between simplicity and completeness. Although I think the change to && would make the test fail (can? would be called just once), I see the point, I'm just not sure how much more detail we want to put in there, given the complete matrix in all its combinations is probably too much.

I think the real fix would be to redesign the admin menu items to have sub-items wrapped in objects and check if any of those is #visible?, but this is clearly beyond the scope of this PR.

That'd be fine if I opened an issue describing the underlying problem?

Copy link
Member

@aldesantis aldesantis Oct 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I think the change to && would make the test fail (can? would be called just once)

Right, my bad. 🤦

I think the real fix would be to redesign the admin menu items to have sub-items wrapped in objects and check if any of those is #visible?, but this is clearly beyond the scope of this PR.

Agreed that it would be better. Feel free to open an issue.

I see the point, I'm just not sure how much more detail we want to put in there, given the complete matrix in all its combinations is probably too much.

I feel like we could do a bit better than the current implementation without writing too much additional code.

A good compromise could be just two tests such as the following:

it 'is shown if any of its submenus are present' do
  view = double("view")
  allow(view).to receive(:can?).and_return(true, false)

  result = view.instance_exec(&menu_item.condition)

  expect(result).to eq(true)
end

it 'is not shown if none of its submenus are present' do
  view = double("view", can?: false)

  result = view.instance_exec(&menu_item.condition)

  expect(result).to eq(false)
end

The main benefits over the existing tests, in my opinion, are:

  1. You're testing the underlying logic more directly. Rather than setting an expectation on how many times can? is called, you're setting it on whether the condition works properly.
  2. The tests are more expressive. Reading your test, it's unclear how the underlying implementation works. With this version, it's a bit clearer what's going on in the implementation.
  3. The tests follow a standard setup/exercise/verify pattern and are easier for a human to parse. The origin.al test mixed setup and expectations by using expect(view).to receive(...).

Let me know your thoughts!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion, it was useful!
The issue is here #3785

It's possible to create permission sets the only give access to a
subset of the settings menu items, without this the settings menu was
kept hidden.
@elia elia force-pushed the elia/fix-settings-menu-condition branch from 6e20af2 to 295df5e Compare October 2, 2020 09:13
@elia elia marked this pull request as ready for review October 2, 2020 10:49
@elia elia requested a review from aldesantis October 2, 2020 10:50
@aldesantis aldesantis requested a review from a team October 2, 2020 11:50
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @elia!

@kennyadsl kennyadsl merged commit 10a1199 into solidusio:master Oct 5, 2020
@kennyadsl kennyadsl deleted the elia/fix-settings-menu-condition branch October 5, 2020 09:44
@kennyadsl kennyadsl added the changelog:solidus_backend Changes to the solidus_backend gem label Oct 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:solidus_backend Changes to the solidus_backend gem
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants